package com.zb.excel.importExcel.controller;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.entity.AccounLoginInfo;
import com.zb.excel.importExcel.service.ConImportExcelService;
import com.zb.result.Result;
import com.zb.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;

/**
 * @Author WEN
 * @Date 2024/1/5 0005
 **/
@Api(tags = "导入类接口")
@RestController
@RequestMapping("/import")
public class ImportExcelController {
    @Resource
    private ConImportExcelService conImportExcelService;

    @ApiOperation("客户档案导入")
    @PostMapping("/userExcelImport/{uuid}")
    public Result userExcelImport(HttpServletRequest request,
                                  @PathVariable @ApiParam(value = "uuid进度识别码", required = true) String uuid,
                                  @RequestPart(value = "file") @ApiParam(value = "文件") MultipartFile file) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conImportExcelService.userExcelImport(loginInfo, uuid, file);
        return Result.success("客户档案正在导入中...");
    }

    @ApiOperation("商品资料导入")
    @PostMapping("/goodsExcelImport/{type}/{uuid}")
    public Result goodsExcelImport(HttpServletRequest request,
                                   @PathVariable @ApiParam(value = "商品类型：1-成品，2-配件", required = true, example = "1") int type,
                                   @PathVariable @ApiParam(value = "uuid进度识别码", required = true) String uuid,
                                   @RequestPart(value = "file") @ApiParam(value = "文件") MultipartFile file) {
        Assert.isTrue(Pattern.matches("^(1|2)$", type + ""), "商品类型参数不合法");
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conImportExcelService.goodsExcelImport(loginInfo, type, uuid, file);
        return Result.success("商品资料正在导入中...");
    }

    @ApiOperation("工程师资料导入")
    @PostMapping("/networkEmpImport/{uuid}")
    public Result networkEmpImport(HttpServletRequest request,
                                   @PathVariable @ApiParam(value = "uuid进度识别码", required = true) String uuid,
                                   @RequestPart(value = "file") @ApiParam(value = "导入文件") MultipartFile file) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conImportExcelService.networkEmpImport(loginInfo, uuid, file);
        return Result.success("工程师资料正在导入中...");
    }

    @ApiOperation("服务商资料导入")
    @PostMapping("/networkImport/{uuid}")
    public Result networkImport(HttpServletRequest request,
                                @PathVariable @ApiParam(value = "uuid进度识别码", required = true) String uuid,
                                @RequestPart(value = "file") @ApiParam(value = "导入文件") MultipartFile file) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conImportExcelService.networkImport(loginInfo, uuid, file);
        return Result.success("服务商资料正在导入中...");
    }

    @ApiOperation("工单数据导入")
    @PostMapping("/orderImport/{uuid}")
    public Result orderImport(HttpServletRequest request, @PathVariable @ApiParam(value = "uuid进度识别码", required = true) String uuid,
                              @RequestPart(value = "file") @ApiParam(value = "导入文件") MultipartFile file) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conImportExcelService.orderImport(loginInfo, uuid, file);
        return Result.success("工单数据正在导入中...");
    }

    @ApiOperation("一物一码数据导入")
    @PostMapping("/codeImport/{uuid}")
    public Result codeImport(HttpServletRequest request, @PathVariable @ApiParam(value = "uuid进度识别码", required = true) String uuid ,
                             @RequestPart(value = "file") @ApiParam(value = "导入文件") MultipartFile file) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conImportExcelService.codeImport(loginInfo, uuid, file);
        return Result.success("一物一码数据正在导入中");
    }


}
